<?php

namespace App\Http\Controllers;

use App\Log;

class SMSService {
  use CurlTrait;
  public function getHeader($method){
    //get username and password
    $config = \App::make('config')->get('services.stm_sms');
    extract($config);
    $authString = "Authorization: Basic ".base64_encode($user.":".$key);
    return array("Content-Type: application/json","X-HTTP-Method-Override: $method",$authString);
  }
  public function sendSMS($params) {

    //we don't send the sms at all
    if (config('app.debug')) {
      return '1111';
    }
    //获取配置文件
    $config = \App::make('config')->get('services.stm_sms');
    extract($config);

    $url = 'http://studioedu.webpowerchina.cn/sms/rest/v1/sms';
    $method = 'post';
    $header = $this->getHeader($method);
    // $header = is_array($header);
    $param = array(
            'campaignID'=>1,
            'mobile' => $params['phone'],
            'content' => $params['message'],
            );
      $param_str = json_encode($param);
     $res_msg = $this->curlCallPost($url,$param_str,['header' => $header]);
    $res_msg = json_decode($res_msg);
        return response()->json($res_msg);

        // not add log now
    // if (isset($res_msg['error'])) {
    //     return false;
    // }

    // $log = new Log;
    // $log->title = '0086' . $params['phone'];
    // $log->message = $res_msg;
    // $log->send_data = $param_str;
    // $log->save();

    // return $res_msg;
  }
}